import Vue from 'vue'
import Router from 'vue-router'
import { getRouter } from './asyncRouter'
import Layout from '@/layout'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/',
    redirect: '/home',
    component: Layout,
    hidden: false,
    children: [
      {
        path: '/home',
        component: () => import('@/views/home/index'),
        meta: { title: '首页' }
      }
    ]
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes.concat(getRouter())
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  const routers = getRouter()
  router.matcher = newRouter.matcher
  router.options.routes = constantRoutes.concat(routers)
}

export default router
